# -*- coding: utf-8 -*-
# @Time: 2023/4/14 20:14
# @File: forms.py
# @Desc: Form表单验证

from wtforms.fields import StringField, IntegerField, FileField
from wtforms import Form, ValidationError
from wtforms.validators import Email, Length, EqualTo, InputRequired
from models.auth import User
from exts import cache
from flask import request
from flask_wtf.file import FileAllowed, FileSize


class BaseForm(Form):
    @property
    def message(self):
        message_list = []
        if self.errors:
            for errors in self.errors.values():
                message_list.extend(errors)
        return message_list


# 用户注册表单类
class RegisterForm(BaseForm):
    email = StringField(validators=[Email(message="请输入正确的邮箱")])
    email_captcha = StringField(validators=[Length(4, 4, message="请输入4位数的邮箱验证码")])
    username = StringField(validators=[Length(3, 20, message="请输入正确格式的用户名,3-20个字符")])
    password = StringField(validators=[Length(6, 20, message="请输入正确格式的密码,6-20个字符")])
    confirm_password = StringField(validators=[EqualTo("password", message="两次输入密码不一致")])
    graph_captcha = StringField(validators=[Length(4, 4, message="请输入正确的图形验证码")])

    def validate_email(self, field):
        email = field.data
        user = User.query.filter_by(email=email).first()
        if user:
            raise ValidationError(message="该邮箱已经被注册!")

    def validate_email_captcha(self, field):
        email_captcha = field.data
        email = self.email.data
        cache_captcha = cache.get(email)  # 读取缓存中存储的验证码
        if not cache_captcha or email_captcha != cache_captcha:
            raise ValidationError(message="邮箱验证码错误!")

    def validate_graph_captcha(self, field):
        key = request.cookies.get("_graph_captcha_key")
        cache_captcha = cache.get(key)
        graph_captcha = field.data
        if not cache_captcha or graph_captcha.lower() != cache_captcha.lower():
            raise ValidationError(message="图形验证码错误")


# 用户登录表单类
class LoginForm(BaseForm):
    email = StringField(validators=[Email(message="请输入正确的邮箱")])
    password = StringField(validators=[Length(6, 20, message="请输入正确格式的密码,6-20个字符")])
    remember = IntegerField()


# 用户上传头像表单类
class UploadAvatarForm(BaseForm):
    image = FileField(validators=[
        FileAllowed(['jpg', 'png', 'jpeg'], message="图像格式仅支持jpg、jpeg、png"),
        FileSize(max_size=5 * 1024 * 1024, message="图像最大不能超过5M")])


# 用户信息表单类
class ProfileForm(BaseForm):
    signature = StringField(validators=[Length(min=1, max=50, message="个性签名长度在1-50字之间！")])


# wangEditor上传图片表单类
class UploadImageFrom(BaseForm):
    image = FileField(validators=[
        FileAllowed(['jpg', 'png', 'jpeg'], message="图像格式仅支持jpg、jpeg、png"),
        FileSize(max_size=5 * 1024 * 1024, message="图像最大不能超过5M")])


# 发布帖子表单类
class PublishPostForm(BaseForm):
    title = StringField(validators=[Length(min=1, max=200, message="请输入帖子标题！")])
    board_id = IntegerField(validators=[InputRequired(message="请先选择板块！")])
    content = StringField(validators=[InputRequired(message="帖子内容不能为空！")])


# 发布评论表单类
class PublishCommentForm(BaseForm):
    content = StringField(validators=[InputRequired(message="评论内容不能为空！")])
    post_id = IntegerField(validators=[InputRequired(message="请传入帖子ID！")])